package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.ActivityInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.ActivityInfo;
import com.atguigu.gmall.model.activity.enums.ActivityInfoEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zm
 * @Date: 2021/8/10 22:11
 */
@RestController
@RequestMapping("admin/activity/activityInfo")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;

    /**
     * 获取活动列表 分页
     *
     * @param page
     * @param limit
     * @return
     */
    // http://localhost/admin/activity/activityInfo/1/10
    @GetMapping("{page}/{limit}")
    public Result getActivityInfoList(@PathVariable Long page,
                                      @PathVariable Long limit) {
        Page<ActivityInfo> activityInfoPage = new Page<>(page, limit);
        IPage activityInfoList = activityInfoService.getActivityInfoList(activityInfoPage);
        return Result.ok(activityInfoList);
    }


    /**
     * 获取活动详情
     *
     * @param id
     * @return
     */
    //http://localhost/admin/activity/activityInfo/get/2
    @GetMapping("get/{id}")
    public Result getActivityInfo(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(ActivityInfoEnum.valueOf(activityInfo.getActivityType()).getActivityName());
        return Result.ok(activityInfo);
    }

    /**
     * 保存活动信息
     *
     * @param activityInfo
     * @return
     */
    // http://localhost/admin/activity/activityInfo/save
    @PostMapping("save")
    public Result saveActivityInfo(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok();
    }

    /**
     * 修改活动信息
     *
     * @param activityInfo
     * @return
     */
    // http://localhost/admin/activity/activityInfo/update
    @PutMapping("update")
    public Result updateActivityInfo(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok();
    }


    //http://localhost/admin/activity/activityInfo/remove/5
    @DeleteMapping("remove/{id}")
    public Result removeActivityInfo(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok();
    }


}
